cmake_minimum_required(VERSION 3.20)
project(rustoyc VERSION 0.0.0)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)

find_package(MLIR REQUIRED CONFIG)
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")

include(TableGen)
include(AddLLVM)
include(AddMLIR)
include(HandleLLVMOptions)

include_directories(${LLVM_INCLUDE_DIRS} ${MLIR_INCLUDE_DIRS})
include_directories(${CMAKE_SOURCE_DIR}/Ch4/include)
include_directories(${CMAKE_BINARY_DIR})


set(LLVM_TARGET_DEFINITIONS Ch4/include/toy/Ops.td)
mlir_tablegen(toy/Ops.h.inc -gen-op-decls)
mlir_tablegen(toy/Ops.cpp.inc -gen-op-defs)
mlir_tablegen(toy/Dialect.h.inc -gen-dialect-decls)
mlir_tablegen(toy/Dialect.cpp.inc -gen-dialect-defs)
add_public_tablegen_target(ToyChOpsIncGen)

set(LLVM_TARGET_DEFINITIONS Ch4/mlir/ToyCombine.td)
mlir_tablegen(ToyCombine.inc -gen-rewriters)
add_public_tablegen_target(ToyChCombineIncGen)

set(LLVM_TARGET_DEFINITIONS Ch4/include/toy/ShapeInferenceInterface.td)
mlir_tablegen(toy/ShapeInferenceOpInterfaces.h.inc -gen-op-interface-decls)
mlir_tablegen(toy/ShapeInferenceOpInterfaces.cpp.inc -gen-op-interface-defs)
add_public_tablegen_target(ToyChShapeInferenceInterfaceIncGen)

add_mlir_library(CppSide
  Ch4/mlir/Dialect.cpp
  Ch4/mlir/ToyCombine.cpp
  ShapeInferencePass.cpp
  cpp_side.cpp
)
add_dependencies(CppSide ToyChOpsIncGen ToyChCombineIncGen ToyChShapeInferenceInterfaceIncGen)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/toy)

